package com.huatai.bpmn.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huatai.bpmn.commom.dto.BasicDTO;
import com.huatai.bpmn.commom.util.BaseCommService;
import com.huatai.bpmn.commom.util.TreeTableUtil;
import com.huatai.bpmn.entity.BasicEntity;
import com.huatai.bpmn.mapper.CGlibMapper;
import com.huatai.bpmn.service.BasicService;
import com.huatai.common.api.R;
import com.huatai.common.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  表格表头配置表
 * </p>
 *
 * @author yxy
 * @since 2023-07-07
 */
@RestController
@RequestMapping("/basic")
@Api(value = "表格表头配置表", tags = "表格表头配置表")
public class BasicController {

	@Autowired
	private BaseCommService baseCommService;
	@Autowired
	private BasicService basicService;

	@PostMapping("/treeList")
	@ApiOperation("获取表单表头数据列表   data参数暂时为空，便于后面做条件查询,后续新增条件查询以sk_like_拼接")
	public R treeList(@RequestBody JSONObject data) throws ParseException {
		QueryWrapper queryWrapper = baseCommService.buildQW(data);
		queryWrapper.orderByAsc("sort");
		//把父子数据查询出来
		List<BasicDTO> outList = CGlibMapper.mapperList(basicService.list(queryWrapper), BasicDTO.class);
		List<BasicDTO> doList = TreeTableUtil.list2TreeList(outList, "id", "pid", "children");
		return R.data(doList);
	}

	@PostMapping("/add")
	@ApiOperation("表头字段新增")
	public R add(@RequestBody @Valid BasicEntity data) {
		//如果pid为空时默认为0
		if (StringUtil.isEmpty(data.getPid())){
			data.setPid("0");
		}
		boolean flag = basicService.save(data);
		return flag? R.success("新增成功"): R.fail("新增失败");
	}

	@PostMapping(value = "/update")
	@ApiOperation("表头字段修改")
	public R update(@Valid @RequestBody BasicEntity data) {
		//如果pid为空时默认为0
		if (StringUtil.isEmpty(data.getPid())){
			data.setPid("0");
		}
		boolean flag = basicService.updateById(data);
		return flag? R.success("修改成功"): R.fail("修改失败");
	}

	@DeleteMapping("/remove/{id}")
	@ApiOperation("表头字段删除")
	public R remove(@PathVariable("id") String id) {

		boolean flag=basicService.removeData(id);
		return flag? R.success("删除成功"): R.fail("删除失败");
	}

	@GetMapping("/getColum/{place}")
	@ApiOperation("根据表单标识获取表头字段")
	public R getColum(@PathVariable("place") String place) {
		return R.data(basicService.getColum(place));
	}

	@GetMapping("/getExternalParameterColum")
	@ApiOperation("获取外部参数表头和数据")
	public R getExternalParameterColum(@RequestParam String deployId) {
		Map<String,Object> map=basicService.getExternalParameterColum(deployId);
		return R.data(map);
	}

}

